import { _decorator, Component, instantiate, Label, Layout, Node, Slider, Sprite, Toggle, Vec2, Vec3 } from 'cc';
import { UIManager } from '../UIManager';
import { LanauageManager } from '../LanauageManager';
import { GameData, inviteRewardCfg } from '../GameData';
import { HttpClient } from '../net/HttpClient';
import { EventManager } from '../EventManager';
import { AudioMgr } from '../AudioMgr';
import { inviteItem } from '../item/inviteItem';
const { ccclass, property } = _decorator;

@ccclass('popupInvite')
export class popupInvite extends Component {

    @property(Node)
    layout:Node = null;

    @property(Label)
    myCodeLab:Label = null;

    @property(Label)
    yesterSumLab:Label = null;

    @property(Label)
    yesInvitRewardLab:Label = null;

    protected start(): void {
        EventManager.eventTarget.on(EventManager.REFRESH_GAME, this.refresh, this);
    }

    onEnable() {
        this.refresh();
    }

    refresh(){
        if (!this.node.active){
            return
        }

         for (let index = 0; index < this.layout.children.length; index++) {
            const element = this.layout.children[index];
            element.active = false;
        }

        for (let index = 0; index < GameData.inviteRewardCfgList.length; index++) {
            let itemData:inviteRewardCfg = GameData.inviteRewardCfgList[index];
            let item = this.layout.children[index];
            if (!item){
                item = instantiate(this.layout.children[0])
                item.parent = this.layout;
            }
            item.active = true;
            item.getComponent(inviteItem).refresh(itemData,index+1);
        }

        this.myCodeLab.string = GameData.getIns().userData.code;

        this.yesterSumLab.string = GameData.getIns().userData.inviteRewardSum + "";

        this.yesInvitRewardLab.string = GameData.getIns().userData.inviteRewardYesterday + "";
    }

    onClickHandler(event:Event, str:string){
        if (str == "getReward"){
            if (GameData.getIns().userData.inviteRewardYesterday <= 0){
                UIManager.showTip("暂无奖励可领")
                return;
            }

            HttpClient.getInstance().sendGetInviteReward(()=>{
                UIManager.showTip("领取成功")
            })
        }
        else if (str == "share"){
            UIManager.open(UIManager.uiNamePath.popupKefu);
        }
        else if (str == "myInvite"){
            UIManager.open(UIManager.uiNamePath.popupMyInvit);
        }
        else if (str == "history"){
            UIManager.open(UIManager.uiNamePath.popupInvitHistory);
        }
    }
}

